home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7361 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.5 KB  |  108 lines

  1. Path: news.netgate.net!news
  2. From: Tamara Johnson <malihini@netgate.net>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Question - C (beginner)
  5. Date: Sun, 25 Feb 1996 13:42:16 -0800
  6. Organization: NetGate Communications
  7. Message-ID: <3130D7B8.65B6@netgate.net>
  8. References: <312FBA11.3946@netgate.net>
  9. NNTP-Posting-Host: d6.netgate.net
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0b6a (Win95; I)
  14.  
  15. Tamara Johnson wrote:
  16. > Problem: not correctly counting chars in
  17. > each word, not correctly counting words
  18. > with >0 && <7 chars and words with >6 chars.
  19. > Suggestions greatly appreciated,
  20. > [stuff deleted] 
  21.  
  22. Got it to count chars in words but I doubt this is the best/most 
  23. efficient way...
  24.  
  25. suggestions greatly appreciated,
  26. Tamara
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include <string.h>
  30. /* ---------------------- defines ------- */
  31. #define YES 1
  32. #define NO  0
  33. #define MAX 80
  34. /* ---------- function prototypes ------- */
  35. void Input();
  36. int NumWords();
  37. void OverUnder();
  38. /* --------------- global objects ------- */
  39. char test_string[MAX];
  40. int under, over, n;
  41. /* -------------------------main--------- */
  42. void main()
  43.   {
  44.   Input();
  45.  
  46.   printf("Number of words in the string: %d\n", 
  47.           NumWords());
  48.   OverUnder();
  49.   printf("Number of words with 7 or more char: %d\n"
  50.          "Number of words with 6 or less char: %d\n",
  51.           over, under); 
  52.   }
  53. /* ----get a character string input------ */
  54. void Input()
  55.   {
  56.   char buffer[300];
  57.  
  58.   printf("Enter test string: ");
  59.   gets(buffer);
  60.   strncpy(test_string, buffer, MAX);
  61.   test_string[(MAX-1)]='\0';
  62.   n = (strlen(test_string)+1);    /* add 1 to get end null */
  63.   }
  64. /* ----get number of words in the string-- */
  65. int NumWords()
  66. {
  67.   int i, word_count, inword;
  68.  
  69.   inword=NO;
  70.   word_count=0;
  71.  
  72.   for(i=0;i<n;i++)        /* use of n shortens loop */
  73.     {
  74.     if(test_string[i]==' '||test_string[i]=='\n'
  75.        ||test_string[i]=='\0'||test_string[i]=='\t')
  76.       inword=NO;
  77.     else if(inword==NO)
  78.       {
  79.       inword=YES;
  80.       word_count++;
  81.       }
  82.     }
  83.   return word_count;
  84. }
  85. /* count the nuber of char in each word and
  86.    find qty less than or equal to 6 
  87.    and equal or greater than 7 */
  88. void OverUnder()
  89. {
  90. int i, char_count;
  91.  
  92. char_count=0;
  93.  
  94.   for(i=0;i<n;i++)
  95.     {
  96.     char_count++;
  97.     if(test_string[i]==' '||test_string[i]=='\n'
  98.        ||test_string[i]=='\0'||test_string[i]=='\t')
  99.                         {
  100.       if(((char_count-1)>0)&&((char_count-1)<7)) under++;
  101.       if((char_count-1)>6) over++;
  102.       char_count=0;
  103.                         }
  104.     }
  105. }
  106.